package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 *
 * @author zzq
 * @created 2025-09-29 10:32
 * @description 部门管理业务层实现类
 *
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    @Override
    public List<Dept> list() {
        return deptMapper.list();
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public void update(Dept dept) {
        dept.setUpdateTime(LocalDateTime.now());
        deptMapper.update(dept);
    }

    @Override
    public Dept getById(Integer id) {
        return deptMapper.selectById(id);
    }

    @Override
    public PageResult<Dept> page(String name, Integer status, Integer page, Integer pageSize) {
        PageHelper.startPage(page, pageSize);
        //2 执行查询
        List<Dept> list = deptMapper.list(name, status);
        //3.数据封装
        // 条件查询列表强转为Page类型
        Page<Dept> rs =  (Page<Dept>) list;
        // 数据封装
        return PageResult.<Dept>builder()
                .total(Long.valueOf(rs.getTotal()).intValue())
                .rows(rs.getResult())
                .build();
    }

    @Override
    @SuppressWarnings("all")
    public void addDept(Dept dept) {
        // 设置部门创建时间
        dept.setCreateTime(LocalDateTime.now());
        // 设置部门更新时间
        dept.setUpdateTime(LocalDateTime.now());
        // 调用DAO
        deptMapper.insert(dept);

    }
}